home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C035.ZIP / JMODEM_F.C < prev    next >
Text File  |  1990-02-17  |  18KB  |  451 lines

  1. /****************************************************************************/
  2. /*   FILE JMODEM_F.C                                                        */
  3. /*   Created 11-JAN-1990                    Richard B. Johnson              */
  4. /*                                          405 Broughton Drive             */
  5. /*                                          Beverly, Massachusetts 01925    */
  6. /*                                          BBS (508) 922-3166              */
  7. /*                                                                          */
  8. /*   screen();         All screen output procedures. Uses INT 10H under     */
  9. /*                     MS-DOS.                                              */
  10. /*                                                                          */
  11. /*   These routines are absolutely not necessary and could be replaced      */
  12. /*   with _printf() statements. They are used to make the pretty screens    */
  13. /*   and overlapping windows that the PC community has grown to expect.     */
  14. /*   I didn't spend a lot of time on documentation.  _malloc() is used      */
  15. /*   to obtain memory for saving the screen-content. A failure to obtain    */
  16. /*   sufficient memory will not abort the program, just mess up the screen. */
  17. /*                                                                          */
  18. /****************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <dos.h>
  22. #include <stdlib.h>
  23. #include <malloc.h>
  24. #include "screen.h"
  25. #include "jmodem.h"
  26. short sav_par[56];                               /* Save row/columns       */
  27. short *buffer[6];                                /* 6 pointers for 6 boxes */
  28. short last_box;                                  /* Last box on the screen */
  29. short next_line;                                 /* Next line to print     */
  30. short start_txt;
  31. short start_row;
  32. short start_col;
  33. short end_row;
  34. short end_col;
  35.  
  36. char  *signon[] = {
  37.                  "     J M O D E M ",
  38.                  "File transfer protocol",
  39.                  "     "VERS
  40.                  };
  41.  
  42. char *sta_blk[] = {
  43.                   "   Block : " ,
  44.                   "  Length : " ,
  45.                   "   Bytes : " ,
  46.                   "Rate CPS : " ,
  47.                   "  Status : " ,
  48.                   "Synchronizing ...", 
  49.                   "  Receiving file ",
  50.                   "Transmitting file"
  51.                   };
  52.  
  53. char *fil_blk[] = {
  54.                   "Opening file ",
  55.                   "Can't open the file!",
  56.                   "Open okay",
  57.                   "File exists, renaming to ",
  58.                   "Can't create the file!",
  59.                   };
  60.  
  61. /****************************************************************************/
  62. short screen (function,sys, text )
  63. short function;
  64. SYS  *sys;
  65. char *text;
  66. {
  67.     short port=1;
  68.     union REGS bios;                                /* For int 10H         */
  69.     char string[80];                                /* Messages in windows */
  70.     short page;
  71.     unsigned screen;
  72.     short i;
  73.  
  74.     bios.x.ax =                                        /* Initialize       */
  75.     bios.x.bx =
  76.     bios.x.cx =
  77.     bios.x.dx = 0;
  78.  
  79.     switch (function)
  80.     {
  81.         case SCR_SGN:
  82.         {
  83.             page=0;
  84.             kill_curs(&bios);
  85.             screen    = attribute[page].win | 0x20;
  86.             start_row = box_loc [(page * 4 ) + 0];
  87.             start_col = box_loc [(page * 4 ) + 1];
  88.             end_row   = box_loc [(page * 4 ) + 2];
  89.             end_col   = box_loc [(page * 4 ) + 3];
  90.             start_txt = start_row + 1;
  91.             set_box (page     ,                        /* Page             */
  92.                      screen   ,                        /* Screen attribute */
  93.                      start_row,                        /* Start row        */
  94.                      start_col,                        /* Start column     */
  95.                      end_row  ,                        /* End row          */
  96.                      end_col  ,                        /* End column       */
  97.                      sav_par  ,
  98.                      buffer   ,
  99.                      &bios  );
  100.             for (i = 0; i<3; i++)
  101.             {
  102.                 set_curs (start_txt+i,start_col + 4,&bios);
  103.                 sprintf(string,"%s",signon[i]);
  104.                 write_str(string,attribute[page].txt,&bios);
  105.             }
  106.             last_box = page;
  107.             break;
  108.         }
  109.     case SCR_FIL:
  110.         {
  111.             page=1;
  112.             screen    = attribute[page].win | 0x20;
  113.             start_row = box_loc [(page * 4 ) + 0];
  114.             start_col = box_loc [(page * 4 ) + 1];
  115.             end_row   = box_loc [(page * 4 ) + 2];
  116.             end_col   = box_loc [(page * 4 ) + 3];
  117.             start_txt = start_row + 1;
  118.             set_box (page     ,                        /* Page             */
  119.                      screen   ,                        /* Screen attribute */
  120.                      start_row,                        /* Start row        */
  121.                      start_col,                        /* Start column     */
  122.                      end_row  ,                        /* End row          */
  123.                      end_col  ,                        /* End column       */
  124.                      sav_par  ,
  125.                      buffer   ,
  126.                      &bios  );
  127.             set_curs (start_txt++,start_col + 4,&bios);
  128.             write_str(fil_blk[0],attribute[page].txt,&bios);
  129.             write_str(text,attribute[page].txt,&bios);
  130.             last_box=page;
  131.             break;
  132.         }
  133.     case SCR_FNF:
  134.         {
  135.             page = last_box;
  136.             set_curs (start_txt++ ,start_col + 4, &bios);
  137.             write_str(fil_blk[1],attribute[page].txt,&bios);
  138.             break;
  139.         }
  140.     case SCR_FOK:
  141.         {
  142.             page = last_box;
  143.             set_curs (start_txt++, start_col + 4, &bios);
  144.             write_str(fil_blk[2],attribute[page].txt,&bios);
  145.             break;
  146.         }
  147.     case SCR_STA:
  148.         {
  149.             page = 2;
  150.             screen    = attribute[page].win | 0x20;
  151.             start_row = box_loc [(page * 4 ) + 0];
  152.             start_col = box_loc [(page * 4 ) + 1];
  153.             end_row   = box_loc [(page * 4 ) + 2];
  154.             end_col   = box_loc [(page * 4 ) + 3];
  155.             start_txt = start_row + 1;
  156.             set_box (page     ,                        /* Page             */
  157.                      screen   ,                        /* Screen attribute */
  158.                      start_row,                        /* Start row        */
  159.                      start_col,                        /* Start column     */
  160.                      end_row  ,                        /* End row          */
  161.                      end_col  ,                        /* End column       */
  162.                      sav_par  ,
  163.                      buffer   ,
  164.                      &bios  );
  165.  
  166.             for (i=0; i<6; i++)
  167.             {
  168.                 set_curs (start_txt+i,start_col + 4,&bios);
  169.                 write_str(sta_blk[i],attribute[page].txt,&bios);
  170.             }
  171.             last_box = page;
  172.             break;
  173.         }
  174.    case SCR_FRN:
  175.         {
  176.             page = last_box;
  177.             set_curs (start_txt++, start_col + 4, &bios);
  178.             write_str(fil_blk[3],attribute[page].txt,&bios);
  179.             write_str(text,attribute[page].txt,&bios);
  180.             break;
  181.         }
  182.    case SCR_FCR:
  183.         {
  184.             page = last_box;
  185.             set_curs (start_txt++, start_col + 4, &bios);
  186.             write_str(fil_blk[4],attribute[page].txt,&bios);
  187.             break;
  188.         }
  189.    case SCR_SYR:
  190.         {
  191.             page = last_box;
  192.             start_txt = start_row + 1;
  193.             set_curs (start_txt + 5, start_col + 4, &bios);
  194.             write_str(sta_blk[6],attribute[page].txt | 0x8000 ,&bios);
  195.             break;
  196.         }
  197.    case SCR_SYT:
  198.         {
  199.             page = last_box;
  200.             start_txt = start_row + 1;
  201.             set_curs (start_txt + 5, start_col + 4, &bios);
  202.             write_str(sta_blk[7],attribute[page].txt | 0x8000,& bios);
  203.             break;
  204.         }
  205.     case SCR_SYS:
  206.         {
  207.         page = last_box;
  208.         sprintf(string,"%-6d",sys->s_blk);
  209.         set_curs (start_txt, start_col + 15, &bios);
  210.         write_str(string,attribute[page].txt,& bios);
  211.  
  212.         sprintf(string,"%-4d",sys->s_len);
  213.         set_curs (start_txt + 1, start_col + 15, &bios);
  214.         write_str(string,attribute[page].txt,& bios);
  215.  
  216.         sprintf(string,"%-9lu",sys->s_byt);
  217.         set_curs (start_txt + 2, start_col + 15, &bios);
  218.         write_str(string,attribute[page].txt,& bios);
  219.  
  220.         sprintf(string,"%-4d",sys->s_cps);
  221.         set_curs (start_txt + 3, start_col + 15, &bios);
  222.         write_str(string,attribute[page].txt,& bios);
  223.  
  224.         sprintf(string,"%s",sys->s_sta);
  225.         set_curs (start_txt + 4, start_col + 15, &bios);
  226.         write_str(string,attribute[page].txt,& bios);
  227.         break;
  228.         }
  229.     case SCR_END:
  230.         {
  231.         for (page = last_box; page >=0; page--)
  232.             {
  233.             end_box (page,sav_par,buffer,&bios);
  234.             }
  235.         restore_curs(&bios);
  236.         break;
  237.         }
  238.     }
  239.     return 0;
  240. }
  241. /****************************************************************************
  242.    Save screen contents in a buffer obtained from _malloc. Write a border and
  243.    screen attributes to saved screen location. Record the address of the
  244.    buffer so the screen contents may be restored. Global *buffer[] is used
  245.    to save the pointers.
  246. */
  247. short set_box (page  ,                           /* Box number             */
  248.              screen,                             /* Screen attribute       */
  249.              start_row,                          /* Start row of border    */
  250.              start_col,                          /* Start column of border */
  251.              end_row,                            /* End row of border      */
  252.              end_col,                            /* End column of border   */
  253.              sav_par,
  254.              buffer,
  255.              bios)
  256. short page;
  257. unsigned short screen;
  258. short start_row;
  259. short start_col;
  260. short end_row;
  261. short end_col;
  262. short sav_par[];
  263. short *buffer[];
  264. union REGS *bios;
  265. {
  266.     unsigned short putscr;
  267.     short sav_col;
  268.     short sav_row;
  269.     short row;
  270.     short col;
  271.     get_curs(bios);                                 /* Get cursor position  */
  272.     sav_row = (short) bios->h.dh;                   /* Save cursor row      */
  273.     sav_col = (short) bios->h.dl;                   /* Save cursor column   */
  274.     sav_par[(page * 7) + 0] = (short) screen;
  275.     sav_par[(page * 7) + 1] = start_row;
  276.     sav_par[(page * 7) + 2] = start_col;
  277.     sav_par[(page * 7) + 3] = end_row;
  278.     sav_par[(page * 7) + 4] = end_col;
  279.     sav_par[(page * 7) + 5] = sav_row;
  280.     sav_par[(page * 7) + 6] = sav_col;
  281.  
  282.     buffer[page] = (short*) malloc (     /* Get pointer to memory and save */
  283.                    ((end_row - start_row)
  284.                   * (end_col - start_col))
  285.                   *  sizeof (short) );
  286.  
  287.     if (buffer[page] == NULL)
  288.         {
  289.         puts("\nMemory allocation failed!");
  290.         return(1);
  291.         }
  292.    for (row= start_row; row < end_row; row++)
  293.         {
  294.         for (col=start_col; col < end_col; col++)
  295.             {
  296.             set_curs (row,col,bios);                    /* Set cursor pos */
  297.             *buffer[page] = get_char_atr(bios);         /* Save char/attr */
  298.             buffer[page]++;                             /* Bump pointer   */
  299.             putscr = screen;                            /* default (else) */
  300.             if (row == start_row || row == end_row-1)   /* Top and bottom */
  301.             putscr = (screen & 0x0FF00) | 205;
  302.             if (col == start_col || col == end_col-1)   /* Right and left */
  303.             putscr = (screen & 0x0FF00) | 186;
  304.             if (row == start_row && col == start_col)   /* NW corner      */
  305.             putscr = (screen & 0x0FF00) | 201;
  306.             if (row == start_row && col == end_col-1)   /* NE corner      */
  307.             putscr = (screen & 0x0FF00) | 187;
  308.             if (row == end_row-1 && col == start_col)   /* SW corner      */
  309.             putscr = (screen & 0x0FF00) | 200;
  310.             if (row == end_row -1 && col == end_col-1)  /* SE corner      */
  311.             putscr = (screen & 0x0FF00) | 188;
  312.             set_char_atr (putscr,bios);                 /* Write to screen */
  313.             }
  314.         }
  315.     return(0);
  316. }
  317. /****************************************************************************
  318. /* Restore the screen contents saved in memory pointed to by global *buffer[].
  319. */
  320. short end_box (page,sav_par,buffer,bios)
  321. short page;
  322. short sav_par[];
  323. short *buffer[];
  324. union REGS *bios;
  325. {
  326.     unsigned screen;
  327.     short start_row;
  328.     short start_col;
  329.     short end_row;
  330.     short end_col;
  331.     short row;
  332.     short col;
  333.     short sav_row;
  334.     short sav_col;
  335.  
  336.     screen    = (unsigned) sav_par[(page * 7) + 0];  /* Restore from array */
  337.     start_row = sav_par[(page * 7) + 1];
  338.     start_col = sav_par[(page * 7) + 2];
  339.     end_row   = sav_par[(page * 7) + 3];
  340.     end_col   = sav_par[(page * 7) + 4];
  341.     sav_row   = sav_par[(page * 7) + 5];
  342.     sav_col   = sav_par[(page * 7) + 6];
  343.  
  344.     buffer[page] -= ( (end_row - start_row)             /* Get buffer start */
  345.                * (end_col - start_col) );
  346.  
  347.     for (row= start_row; row < end_row; row++)
  348.         {
  349.         for (col=start_col; col < end_col; col++)
  350.             {
  351.             set_curs (row,col,bios);                    /* Set cursor pos  */
  352.             set_char_atr(*buffer[page],bios);           /* Restore screen  */
  353.             buffer[page]++;                             /* Bump pointer    */
  354.             }
  355.         }
  356.     free(buffer[page]);                                 /* Free alloc mem  */
  357.     set_curs (sav_row,sav_col,bios);                    /* Restore cursor  */
  358.     return(0);
  359. }
  360. /****************************************************************************/
  361.                         /* Set Cursor to row, column */
  362. void set_curs (row,col,bios)
  363. short row;
  364. short col;
  365. union REGS *bios;
  366. {
  367.     bios->h.ah=(unsigned char)2;         /* Set cursor function  */
  368.     bios->h.dh=(unsigned char)row;       /* Row                  */
  369.     bios->h.dl=(unsigned char)col;       /* Column               */
  370.     bios->h.bh=(unsigned char)0;         /* Page 0               */
  371.     int86(VIDEO, bios, bios);            /* Use for in and out   */
  372.     return;
  373. }
  374. /****************************************************************************/
  375. void get_curs(bios)
  376. union REGS *bios;
  377. {
  378.     bios->h.ah=(unsigned char)3;         /* Get cursor function  */
  379.     bios->h.bh=(unsigned char)0;         /* Page 0               */
  380.     int86(VIDEO, bios, bios);            /* Use for in and out   */
  381.     return;
  382. }
  383. /****************************************************************************/
  384. void kill_curs(bios)
  385. union REGS *bios;
  386. {
  387.     bios->h.ah=(unsigned char)1;         /* Set cursor function  */
  388.     bios->h.bh=(unsigned char)0;         /* Page 0               */
  389.     bios->x.cx=(unsigned short) 0xFFFF;  /* Kill the cursor      */
  390.     int86(VIDEO, bios, bios);            /* Use for in and out   */
  391.     return;
  392. }
  393. /****************************************************************************/
  394. void restore_curs(bios)
  395. union REGS *bios;
  396. {
  397.     bios->h.ah=(unsigned char)1;         /* Get cursor function  */
  398.     bios->h.bh=(unsigned char)0;         /* Page 0               */
  399.     bios->x.cx=(unsigned short) 0x0607;  /* Restore the cursor   */
  400.     int86(VIDEO, bios, bios);            /* Use for in and out   */
  401.     return;
  402. }
  403. /****************************************************************************/
  404.                 /* Write char /attr at cursor position */
  405. void set_char_atr(atr_chr,bios)
  406. unsigned atr_chr;
  407. union REGS *bios;
  408. {
  409.     bios->h.ah=(unsigned char)9;                 /* Write char function  */
  410.     bios->h.al=(unsigned char) (atr_chr & 0xFF); /* Character            */
  411.     bios->x.cx=1;                                /* One character        */
  412.     bios->h.bl=(unsigned char) (atr_chr >> 8);   /* Attribute            */
  413.     bios->h.bh=(unsigned char)0;                 /* Page 0               */
  414.     int86(VIDEO, bios, bios);                    /* Use for in and out   */
  415.     return;
  416. }
  417. /****************************************************************************/
  418.                 /* Get char /attr at cursor position */
  419. unsigned get_char_atr(bios)
  420. union REGS *bios;
  421. {
  422.     bios->h.ah=(unsigned char)8;                 /* Read char function   */
  423.     bios->h.bh=(unsigned char)0;                 /* Page 0               */
  424.     int86(VIDEO, bios, bios);                    /* Use for in and out   */
  425.     return (bios->x.ax);                         /* Its in the AX regis  */
  426.  }
  427. /****************************************************************************/
  428. void write_str(string,atr,bios)
  429. char string[];
  430. unsigned atr;
  431. union REGS *bios;
  432.  
  433. {
  434.     short i= 0;
  435.     short row;
  436.     short col;
  437.     while (string[i])                            /* Until the NULL      */
  438.     {
  439.     set_char_atr(atr|(short) string[i++],bios);  /* Write char and attr */
  440.     get_curs(bios);                              /* Get cursor position */
  441.     row = (short) bios->h.dh;                    /* Get row             */
  442.     col = (short) bios->h.dl;                    /* Get column          */
  443.     col++;                                       /* Next column         */
  444.     set_curs (row,col,bios);                     /* Set column          */
  445.     }
  446.     return;
  447. }
  448. /****************************************************************************/
  449. /*                       E N D  O F  M O D U L E                            */
  450. /****************************************************************************/
  451.